﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


	public class CookiesManager
	{
		static String CookiesKey = "AgitoCookie";
		static String UserName = "eu";
		static String Password = "dp";

		/// <summary>
		/// Reads cookies from reques
		/// </summary>
		/// <param name="request">Current reques</param>
		/// <returns>Returns String[] { userName, password } if userName or password is empty, returns null</returns>
		public static String[] ReadCookie(HttpRequest request)
		{
			HttpCookie cookie = request.Cookies[CookiesKey];

			return cookie == null ? null : new String[] { cookie[UserName], cookie[Password] };
		}

		/// <summary>
		/// Adds new cookie with user identity
		/// </summary>
		/// <param name="response">Currenct response</param>
		/// <param name="userName">User name</param>
		/// <param name="password">User password</param>
		public static void WriteCookie(HttpResponse response, String userName, String password)
		{
			HttpCookie cookie = new HttpCookie(CookiesKey);
			cookie.Values.Add(UserName, userName);
			cookie.Values.Add(Password, password);
			cookie.Expires = DateTime.Now.AddDays(10);

			response.Cookies.Add(cookie);
		}

		/// <summary>
		/// Deletes cookie
		/// </summary>
		/// <param name="response">Current response</param>
		public static void DeleteCoolie(HttpResponse response)
		{
			HttpCookie cookie = new HttpCookie(CookiesKey);
			cookie.Expires = DateTime.Now.AddDays(-1d);
			response.Cookies.Add(cookie);
		}
	}
